package manager

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/manager"
	"gitcode.com/G-YT/baoyou/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type TargetPlanApi struct {
}

// SaveTargetPlan 保存目标计划
// @Tags targetPlan
// @Summary 目标计划
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body manager.TargetPlan true "保存目标计划"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /targetPlan/saveTargetPlan [post]
func (targetPlanApi *TargetPlanApi) SaveTargetPlan(c *gin.Context) {
	var tp manager.TargetPlan
	err := c.ShouldBindJSON(&tp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	hmID := utils.GetHmID(c)
	if hmID == 0 {
		response.FailWithMessage("未获取到登录信息", c)
		return
	}
	tp.HmID = hmID
	if err = TargetPlanService.SaveTargetPlan(&tp); err != nil {
		global.GVA_LOG.Error("保存失败!", zap.Error(err))
		response.FailWithMessage("保存失败", c)
	} else {
		response.OkWithMessage("保存成功", c)
	}
}

// GetTargetPlan 获取目标计划
// @Tags targetPlan
// @Summary 目标计划
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body manager.TargetPlan true "获取目标计划"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /targetPlan/getTargetPlan [get]
func (targetPlanApi *TargetPlanApi) GetTargetPlan(c *gin.Context) {
	id := utils.GetHmID(c)
	if id == 0 {
		response.FailWithMessage("未获取到登录信息", c)
		return
	}
	if result, err := TargetPlanService.GetTargetPlan(id); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(result, c)
	}
}
